package com.yjc.drygoods.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjc.common.utils.PageUtils;
import com.yjc.drygoods.product.entity.CategoryEntity;
import com.yjc.drygoods.product.vo.AttrGroupWithAttrsVo;
import com.yjc.drygoods.product.entity.AttrAttrgroupRelationEntity;
import com.yjc.drygoods.product.entity.AttrEntity;
import com.yjc.drygoods.product.service.AttrAttrgroupRelationService;
import com.yjc.drygoods.product.service.AttrService;
import com.yjc.drygoods.product.service.CategoryService;
import com.yjc.drygoods.product.vo.AttrGroupRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.product.entity.AttrGroupEntity;
import com.yjc.drygoods.product.service.AttrGroupService;
import com.yjc.common.utils.R;


/**
 * 属性分组
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 21:27:48
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * /product/attrgroup/{catelogId}/withattr
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrAttrGroup(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrAttrGroup(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVos);
    }

    /**
     * /product/attrgroup/attr/relation
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
        attrAttrgroupRelationService.saveAttrAttrGroupRelation(attrGroupRelationVos);
        return R.ok();
    }


    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoattrRelation(
            @PathVariable("attrgroupId") Long attrgroupId,
            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoattrRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * /product/attrgroup/attr/relation/delete
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] entity) {
        attrGroupService.deleteRelation(entity);
        return R.ok();
    }


    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> res = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", res);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageByCatelogId(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] list = categoryService.findCatelogPath(catelogId);
        log.info("attrGroupId:{}", attrGroupId);
        log.info("list:{}", list);
        attrGroup.setCatelogPath(list);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        CategoryEntity categoryEntity = categoryService.getById(attrGroup.getCatelogId());
        attrGroup.setCatelogName(categoryEntity.getName());
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
